package com.hnhcyy.b2b.receiver.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {

    
    public final static String payidQueue = "b2b.order.paid";
    
    // 微信支付流水
    public final static String wchatpayFLowQueue = "b2b.order.wechatpay.flow";
    // 支付宝支付流水
    public final static String alipayFLowQueue = "b2b.order.alipay.flow";
    // 中信支付流水
    public final static String citicpayFlowQueue = "b2b.order.citicpay.flow";
    // 银联商务支付流水（扫码）
    public final static String chinaumspayQrQueue = "b2b.order.chinaumspay.qr.flow";
    // 银联商务支付流水（公众号）
    public final static String chinaumspayJsQueue = "b2b.order.chinaumspay.js.flow";
    
    public final static String testQueue = "test";
    
    /**
     * 死信队列：
     */
    public final static String deadQueueName = "dead_queue";
    public final static String deadRoutingKey = "dead_routing_key";
    public final static String deadExchangeName = "dead_exchange";

    /**
     * 死信队列 交换机标识符
     */
    public static final String DEAD_LETTER_EXCHANGE = "x-dead-letter-exchange";
    /**
     * 死信队列交换机绑定键标识符
     */
    public static final String DEAD_LETTER_ROUTING_KEY = "x-dead-letter-routing-key";

    @Bean
    public Queue payidQueue() {
        //将普通队列绑定到死信交换机上
        Map<String, Object> args = new HashMap<>(2);
        args.put(DEAD_LETTER_EXCHANGE, deadExchangeName);
        args.put(DEAD_LETTER_ROUTING_KEY, deadRoutingKey);
        Queue queue = new Queue(payidQueue, true, false, false, args);
        return queue;
    }
    
    @Bean
    public Queue wchatpayFLowQueue() {
        //将普通队列绑定到死信交换机上
        Map<String, Object> args = new HashMap<>(2);
        args.put(DEAD_LETTER_EXCHANGE, deadExchangeName);
        args.put(DEAD_LETTER_ROUTING_KEY, deadRoutingKey);
        Queue queue = new Queue(wchatpayFLowQueue, true, false, false, args);
        return queue;
    }
    @Bean
    public Queue alipayFLowQueue() {
    	//将普通队列绑定到死信交换机上
    	Map<String, Object> args = new HashMap<>(2);
    	args.put(DEAD_LETTER_EXCHANGE, deadExchangeName);
    	args.put(DEAD_LETTER_ROUTING_KEY, deadRoutingKey);
    	Queue queue = new Queue(alipayFLowQueue, true, false, false, args);
    	return queue;
    }
    
    @Bean
    public Queue citicpayFlowQueue() {
        //将普通队列绑定到死信交换机上
        Map<String, Object> args = new HashMap<>(2);
        args.put(DEAD_LETTER_EXCHANGE, deadExchangeName);
        args.put(DEAD_LETTER_ROUTING_KEY, deadRoutingKey);
        Queue queue = new Queue(citicpayFlowQueue, true, false, false, args);
        return queue;
    }
    
    @Bean
    public Queue chinaumspayQrQueue() {
    	//将普通队列绑定到死信交换机上
    	Map<String, Object> args = new HashMap<>(2);
    	args.put(DEAD_LETTER_EXCHANGE, deadExchangeName);
    	args.put(DEAD_LETTER_ROUTING_KEY, deadRoutingKey);
    	Queue queue = new Queue(chinaumspayQrQueue, true, false, false, args);
    	return queue;
    }
    @Bean
    public Queue chinaumspayJsQueue() {
    	//将普通队列绑定到死信交换机上
    	Map<String, Object> args = new HashMap<>(2);
    	args.put(DEAD_LETTER_EXCHANGE, deadExchangeName);
    	args.put(DEAD_LETTER_ROUTING_KEY, deadRoutingKey);
    	Queue queue = new Queue(chinaumspayJsQueue, true, false, false, args);
    	return queue;
    }
    
    @Bean
    public Queue testQueue() {
        Queue queue = new Queue(testQueue, true);
        return queue;
    }
    
    @Bean
    public DirectExchange appExchange() {
        return new DirectExchange("app");
    }
    
    @Bean
    public Binding bindingPaidExchange(Queue payidQueue, DirectExchange appExchange) {
        return BindingBuilder.bind(payidQueue).to(appExchange).with("b2b.order.paid");
    }
    
    @Bean
    public Binding bindingWechatpayFlowExchange(Queue wchatpayFLowQueue, DirectExchange appExchange) {
        return BindingBuilder.bind(wchatpayFLowQueue).to(appExchange).with("b2b.order.wechatpay.flow");
    }
    @Bean
    public Binding bindingAlipayFlowExchange(Queue alipayFLowQueue, DirectExchange appExchange) {
    	return BindingBuilder.bind(alipayFLowQueue).to(appExchange).with("b2b.order.alipay.flow");
    }
    
    @Bean
    public Binding bindingCiticpayFlowQueueExchange(Queue citicpayFlowQueue, DirectExchange appExchange) {
        return BindingBuilder.bind(citicpayFlowQueue).to(appExchange).with("b2b.order.citicpay.flow");
    }
    
    @Bean
    public Binding bindingChinaumspayQrQueueExchange(Queue chinaumspayQrQueue, DirectExchange appExchange) {
        return BindingBuilder.bind(chinaumspayQrQueue).to(appExchange).with("b2b.order.chinaumspay.flow");
    }
    @Bean
    public Binding bindingChinaumspayJsQueueExchange(Queue chinaumspayJsQueue, DirectExchange appExchange) {
    	return BindingBuilder.bind(chinaumspayJsQueue).to(appExchange).with("b2b.order.chinaumspay.js.flow");
    }
    
    /**
     * 死信队列：
     */
    @Bean
    public Queue deadQueue() {
        Queue queue = new Queue(deadQueueName, true);
        return queue;
    }

    @Bean
    public DirectExchange deadExchange() {
        return new DirectExchange(deadExchangeName);
    }

    @Bean
    public Binding bindingDeadExchange(Queue deadQueue, DirectExchange deadExchange) {
        return BindingBuilder.bind(deadQueue).to(deadExchange).with(deadRoutingKey);
    }

}
